﻿// https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=2247488306&idx=2&sn=5430630a560e29bf5a585bf2efdb8391&scene=21#wechat_redirect
// https://leetcode-cn.com/problems/minimum-size-subarray-sum/

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int sum = 0;
        int begin = 0;
        int result = INT32_MAX;
        int win_length = 0;

        for (int end = 0; end < nums.size(); end++) {
            sum += nums[end];

            while (sum >= target) {
                win_length = end - begin + 1;
                result = result < win_length ? result : win_length;
                sum -= nums[begin++];
            }
        }
        return result == INT32_MAX ? 0 : result;
    }
};

int main() {
    Solution obj = Solution();
    vector<int> nums = {2,3,1,2,4,3};
    int target = 7;
    cout << obj.minSubArrayLen(target, nums) << endl;
}

